package com.fingard.dsp.bank.directbank.crbc01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

public class CRBC01Receipt extends CRBC01Base implements IReceipt {

    String receiptMainDir = "";
    @Override
    public RetAtsReceipt qryReceipt(ReqQryReceipt p_reqReceipt) throws Exception {
        //电子回单查询返回ATS的对象
        RetAtsReceipt retAts = new RetAtsReceipt(p_reqReceipt.reqHeader);
        receiptMainDir = FGBiz.dspCenter.dspSet.receiptMainDir;
        if (!StringHelper.isNullOrEmpty(receiptMainDir)) {
            if (!StringHelper.isNullOrEmpty(FGBiz.dspCenter.dspSet.receiptShareDir)) {
                if (!StringHelper.isNullOrEmpty(FGBiz.dspCenter.dspSet.receiptPushServerUrl)) {
                    try {
                        if(this.getBankConfig().getValueAsBool("isOpenReceipt")){
                            XmlTextWriter xmlWriter = new XmlTextWriter();
                            CRBC01Header bankHeader = new CRBC01Header();
                            bankHeader.tranCode = "200007";
                            bankHeader.tranTime = new Date();
                            writeCommonHeader(xmlWriter, bankHeader);
                            xmlWriter.writeStartElement("BODY");
                            xmlWriter.writeElementString("OPEN_FLAG","1");
                            xmlWriter.writeEndElement();//body
                            xmlWriter.writeEndElement();//CRBANK
                            xmlWriter.writeEndDocument();
                            //endregion

                            //region 二：发送请求报文
                            String[] retStrArr = sendToBank(xmlWriter.toXmlString());
                            //endregion

                            //region 三：解析响应报文
                            if (retStrArr[0].length() == 0) {
                                StringBuilder tmpSbMsg = new StringBuilder();
                                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                                if (tmpBkReader != null) {
                                    String tmpRetCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                                    String tmpRetInfo = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                                    if ("000000".equals(tmpRetCode)) {
                                        WriteBankLogLn("电子回单开通成功, 请修改配置文件isOpenReceipt为false并重启本程序");
                                        retAts.respCode = "0";
                                        retAts.respInfo = "电子回单开通成功, 请修改配置文件isOpenReceipt为false并重启本程序";
                                        retAts.writeRetToATS();
                                        return retAts;
                                    } else {
                                        WriteBankLogLn("电子回单开通失败, 原因:"+tmpRetInfo);
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "电子回单开通失败";
                                        retAts.writeRetToATS();
                                        return retAts;
                                    }
                                }else{
                                    WriteBankLogLn("电子回单开通失败, 原因:"+tmpSbMsg);
                                    retAts.respCode = "-1";
                                    retAts.respInfo = "电子回单开通失败";
                                    retAts.writeRetToATS();
                                    return retAts;
                                }
                            }else{
                                WriteBankLogLn("电子回单开通失败, 原因:"+retStrArr[0]);
                                retAts.respCode = "-1";
                                retAts.respInfo = "电子回单开通失败";
                                retAts.writeRetToATS();
                                return retAts;
                            }
                        }

                        downloadReceipt(p_reqReceipt);
                        retAts.respCode = "0";
                        retAts.respInfo = "后台处理中";
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        retAts.respCode = "-1";
                        retAts.respInfo = ex.getMessage();
                        WriteBankLogLn(ex);
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "DSPSystem未配置回单文件推送地址";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "DSPSystem未配置回单文件共享目录";
            }
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = "DSPSystem未配置回单文件下载保存主目录";
        }
        retAts.writeRetToATS();
        return retAts;
    }

    private void downloadReceipt(ReqQryReceipt p_reqReceipt) throws Exception {
        Calendar tmpCalendarFrom = Calendar.getInstance();
        tmpCalendarFrom.setTime(p_reqReceipt.getDateFrom());
        Calendar tmpCalendarTo = Calendar.getInstance();
        tmpCalendarTo.setTime(p_reqReceipt.getDateTo());
        int tmpIntFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
        int tmpIntTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
        StringBuilder fileNameList = new StringBuilder();
        int fileCount = 0;
        while (tmpIntFrom <= tmpIntTo) {
            Date fromDateStep = tmpCalendarFrom.getTime();
            tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
            tmpIntFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
            sendToBank2(p_reqReceipt.actNum, Format.Date8Format.format(fromDateStep));
        }
    }
}
